package org.ws.task.builder;

import org.ws.task.item.ExecuteContext;
import org.ws.task.item.ItemLeaderCommit;
import org.ws.task.item.ItemLeaderProvider;
import org.ws.task.item.PullTask;
import org.ws.task.item.TaskInstance;
import org.ws.task.item.support.SimplePullTask;
import org.ws.task.model.TaskConfig;
import org.ws.task.repository.TaskRepository;

public final class PullTaskBuilder<T> {

    private PullTask<T> task;
    private TaskConfig taskConfig;
    private ItemLeaderProvider<T> provider;
    private ItemLeaderCommit<T> commit;

    protected PullTaskBuilder(TaskConfig taskConfig){
        this.taskConfig = taskConfig;
        this.taskConfig.setTaskType(TaskConfig.TaskType.PULL);
        this.task = new SimplePullTask<>(taskConfig.getName());
    }

    public PullTaskBuilder<T> provider(ItemLeaderProvider<T> provider){
        this.provider = provider;
        return this;
    }

    public PullTaskBuilder<T> commit(ItemLeaderCommit<T> commit){
        this.commit = commit;
        return this;
    }

    public PullTask<T> builder(){
        TaskInstance<T> instance = new TaskInstance<>();
        instance.setTask(this.task);
        instance.setItemLeaderProvider(this.provider);
        if(this.commit == null){
            this.commit = ItemLeaderCommit.DEFAULT;
        }
        instance.setItemLeaderCommit(this.commit);
        instance.setTaskConfig(this.taskConfig);

        TaskRepository.getInstance().addTaskInstance(instance);
        return this.task;
    }
}